/*
 * Created on Oct 22, 2003
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.user;

import org.vectrics.common.db.JdbcTarget;
import org.vectrics.common.db.ProcessException;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;



public class FindNameMatchesJdbc implements JdbcTarget {
	private transient static Logger log = Logger.getLogger(FindNameMatchesJdbc.class);
	//private static SimpleDateFormat sqlDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	private String queryStr = "";
	private HashSet userIds = new HashSet();		

	public FindNameMatchesJdbc(Collection roles, String firstName, String middleName, String lastName) {
		//Vector userSet = new Vector();
        
		String orderClause = " order by u.last_name, u.first_name, u.middle_name";
		String whereClause = "";
		int index = 0;
		if (roles.size() > 0) {
			whereClause += " where";
			whereClause += " (u.user_name = r.user_name) and (";
			Iterator iter = roles.iterator();
			while (iter.hasNext()) {
				String roleName = (String)iter.next();
				if (index > 0)
					whereClause += " or";
				whereClause += " (r.role_name = '" + roleName + "')";
				index++;
			}
			whereClause += ")";
		}
        
		if (lastName != null) {
			String likeEqual = "=";
			if (index > 0)
				whereClause += " and";
			index++;
			if (lastName.indexOf("*") >= 0) {
				likeEqual = "like";
			}
			lastName = lastName.replace('*', '%');
			whereClause += " (u.last_name " + likeEqual + " '" + lastName + "')";
		}
		if (firstName != null) {
			String likeEqual = "=";
			if (index > 0)
				whereClause += " and";
			index++;
			if (firstName.indexOf("*") >= 0) {
				likeEqual = "like";
			}
			firstName = firstName.replace('*', '%');
			whereClause += " (u.first_name " + likeEqual + " '" + firstName + "')";
		}
        
		if (middleName != null) {
			String likeEqual = "=";
			if (index > 0)
				whereClause += " and";
			index++;
			if (middleName.indexOf("*") >= 0) {
				likeEqual = "like";
			}
			middleName = middleName.replace('*', '%');
			whereClause += " (u.middle_name " + likeEqual + " '" + middleName + "')";
		}
        
        
		String queryStr = "select distinct u.id"
				+ " from user_priv r, user u";
		if (whereClause.length() > 0) {
			queryStr += " where "    + whereClause; 
		}
		queryStr += orderClause;
                
		                	
	}
	

	public Set getUserIds() {
		return(userIds);
	}
	
	public void processResults(ResultSet results) throws ProcessException {
		try {
			while (results.next()) {
				String userName = results.getString("id");
				userIds.add(userName);
			}
		}
		catch (java.sql.SQLException se) {
			log.error("getUsersWithRole() - SQL Exception while processing results list", se);
		}
	}

	
	public String getQuery() {
		return(queryStr);		
	}
}

